import sys
import csv
import sqlite3
from operator import itemgetter


def Listar_BD():
    conn = sqlite3.connect("base_de_dados.db")
    c=conn.cursor()

    for i in range(0,11):

        csvFile= open('reb{0}.csv'.format(i),'rb')
   
        spamReader = csv.reader(csvFile,delimiter = ',')
    
        if i == 0:
            c.execute(''' Drop table if exists professores''')
    
            c.execute('''create table if not exists professores(Codigo_do_Docente integer, Nome text,Ultimo_Grau text,Curso text,Categoria text,Regime_de_Tempo integer,\
                         Ano_Lectivo integer,Tipo_de_Estabelecimento text,Nome_Estabelecimento text)''')
    
        spamReader.next()
        for row in spamReader:
            c.execute('''insert into professores(Codigo_do_Docente,nome,Ultimo_Grau,Curso,Categoria,Regime_de_Tempo,Ano_Lectivo,Tipo_de_Estabelecimento,\
                        Nome_Estabelecimento)\
                        values ("{0}", "{1}","{2}", "{3}","{4}", "{5}","{6}", "{7}","{8}")'''.\
                      format(str(row[0]).replace('"','\''),str(row[1]).replace('"','\''),str(row[2]).replace('"','\''),str(row[3]).replace('"','\''),\
                             str(row[4]).replace('"','\''),str(row[5]).replace('"','\''),str(row[6]).replace('"','\''),str(row[7]).replace('"','\''),\
                             str(row[8]).replace('"','\'')))
        conn.commit()
    pass

def Estatistica_1():
    conn = sqlite3.connect("base_de_dados.db")
    c=conn.cursor()
    Lista=[]

    c.execute('''SELECT * FROM professores ''')
    for x in c.fetchall():
        Lista.append(x)
        

    
    sortedLista = sorted(Lista,key=itemgetter(8,7,6,5,4,3,2,1))
    for row in sortedLista:
        print row
        
    return sortedLista 
    
def Estatistica_2():
    
    pass

def Estatistica_3():
    
    pass




